<!-- 品牌管理 -->
<template>
    <div>
        
        <div class="table-page-search-wrapper">
            <a-form layout="inline">
                <a-row :gutter="48">
                     <a-col :md="8" :sm="24">
                        <a-form-item label="手机号">
                            <a-input v-model="getPhone" allow-clear placeholder="请输入手机号" />
                        </a-form-item>
                    </a-col>
                     <a-col :md="8" :sm="24">
                        <!-- <a-form-item label="产品名称">
                            <a-input v-model="productName" allow-clear placeholder="请输入产品名称" />
                        </a-form-item>  -->
                        <a-form-item label="VIP产品名称">
                        <a-select v-model="productName" placeholder="请选择" allowClear>
                            <a-select-option v-for="(item,i) in configList" :key="i" :value="item.productName">{{item.productName}}</a-select-option>
                        </a-select>
                    </a-form-item>
                    </a-col>
                      <a-col :md="8" :sm="24">
                        <a-form-item label="购买订单号">
                            <a-input v-model="orderCode" allow-clear placeholder="请输入购买订单号" />
                        </a-form-item>
                    </a-col>
                      <a-col :md="8" :sm="24">
                        <a-form-item label="支付订单号">
                            <a-input v-model="payOrderCode" allow-clear placeholder="请输入支付订单号" />
                        </a-form-item>
                    </a-col>
                      <a-col :md="8" :sm="24">
                        <a-form-item label="创建日期">
                            <a-range-picker @change="onChange" allowClear/>
                        </a-form-item>
                    </a-col>
                      <a-col :md="8" :sm="12">
                        <a-form-item label="订单类型">
                            <a-select v-model="payType" placeholder="请选择" allowClear @change="getAllData">
                                <a-select-option v-for="(item,i) in payTypeList" :key="i" :value="item.id">{{item.name}}</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col :md="8" :sm="12">
                        <a-form-item label="订单状态">
                            <a-select v-model="orderStatus" placeholder="请选择" allowClear @change="getAllData">
                                <a-select-option v-for="(item,i) in orderStatusList" :key="i" :value="item.id">{{item.name}}</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    
                    <a-col :md="8" :sm="24">
                        <a-form-item >
                            <a-button type="primary" @click="getData()">查询</a-button>
                            <a-button style="margin-left: 20px" icon="plus" @click="add()">新增</a-button>
                        </a-form-item>
                    </a-col>
                </a-row>
            </a-form>
        </div>

        <a-table size="small" rowKey="id" :loading="isload" :columns="columns" :data-source="dataList" @change="handleTableChange" :pagination="{ pageSize: 10, total: total ,showSizeChanger: true,pageSize: size}" :scroll="{ x: 1600 }" >
                <template slot="payType" slot-scope="text, record">
                    <span v-for="(items,i) in payTypeList" >
                        <span v-if="items.id == record.payType">{{items.name}}</span>
                    </span>
                </template>
                 <template slot="orderStatus" slot-scope="text, record">
                    <span v-for="(items,i) in orderStatusList" >
                        <span v-if="items.id == record.orderStatus">{{items.name}}</span>
                    </span>
                </template>
                 <template slot="isOn" slot-scope="text, record">
                       <a-switch checked-children="是" un-checked-children="否" disabled :checked="record.isOn == '1' ? true : false" />
               </template>
            <template slot="operation" slot-scope="text, record">
                
                <div class="editable-row-operations">
                    <!-- <a-button type="link" @click="update(record)">修改</a-button> -->
                    <a-popconfirm
                        title="是否确认删除当前数据?"
                        ok-text="Yes"
                        cancel-text="No"
                        @confirm="del(record)"
                    >
                        <a-button type="link" v-if="record.payType==3"  >删除</a-button>
                    </a-popconfirm>
                </div>
            </template>
        </a-table>

        <a-modal
            title="手动添加VIP会员"
            :visible="visible"
            @ok="handleOk"
            @cancel="visible = false"
            >
            <div>
                <a-form :label-col="{ span: 6 }" :wrapper-col="{ span: 17 }">
                   <a-form-item label="VIP产品">
                        <a-select v-model="vipProductId" placeholder="请选择" allowClear>
                            <a-select-option v-for="(item,i) in configList" :key="i" :value="item.id">{{item.productName}}</a-select-option>
                        </a-select>
                    </a-form-item>
                     <a-form-item label="手机号">
                        <a-input   v-model="phone" placeholder="请输入手机号" allow-clear></a-input>
                    </a-form-item>
                  
                   
                </a-form>
            </div>
        </a-modal>
    </div>
</template>

<script>
import { queryVipOrder,queryVipProduct,updateVipOrderFrom,updateVipOrder } from '@/api/vip';
import { queryBrand }  from '@/api/brand';
import { columns } from './table';
import moment from "moment";
export default {
    data () {
        return {
             brandId: this.global.brandId,
            dataList: [],
            total: 0,
            size: 10,
            page: 1,
            columns,
            isload: false,
            visible: false,
            item: {
                id: '',
                productName:"",//名称	
                activeDay:"",//可用天数	
                price:"",//优惠前价格		
                realPrice:"",//当前价格				
                isOn:1,//是否开启,0:否,1:是	
            },
            isHideList: [
                { id:1, name: '是'},
                { id: 0, name: '否'},
            ],
            payTypeList:[
                {id:1,name:'余额支付'},
                {id:2,name:'支付宝支付'},
                {id:3,name:'手动添加'},
            ],
             orderStatusList:[
                {id:0,name:'待支付'},
                {id:1,name:'支付成功'},
                {id:2,name:'已删除'},
            ],
            vipProductId:"",
            phone:"",
            configList:[],
            getPhone: '',
            payType:"",
            orderStatus:"",
            productName:"",
            orderCode:'',
            payOrderCode:"",
            gtCreateTime: '',
            ltCreateTime:'',
        };
    },
     async created(){
        try {
            this.getAllData()
        } catch (error) {
            this.$notification['error']({
                message: '提示',
                description: '网路丢失，请稍后再试！',
                duration: 8,
            })
        }
    },
   
    methods: {
        moment,
         async getAllData(){
            try {
                    this.configList = await this.getConfig()
                    this.getData()
                } catch (error) {
                    this.$notification['error']({
                        message: '提示',
                        description: '网路丢失，请稍后再试！',
                        duration: 8,
                    })
                }
        },
         getConfig(){
            let _this = this
            return new Promise((resolve,reject)=>{
                const params = {
                    'brandId':_this.brandId,
                }
                queryVipProduct(params)
                .then(res=>{
                    if(res.success){
                        resolve(res.data.content)
                    }else{
                        reject()
                    }
                })
            })
        },
         onChange(date, dateString) {
            console.log(date, dateString);
            this.gtCreateTime = dateString[0]
            this.ltCreateTime = dateString[1]
            this.getData()
        },
        getData(){
            let _this = this
            _this.isload = true
            const params = {
                'brandId':_this.brandId,
                'phone':_this.getPhone,
                'productName':_this.productName,
                'payOrderCode':_this.payOrderCode,
                'orderCode':_this.orderCode,
                'gtCreateTime':_this.gtCreateTime,
                'ltCreateTime':_this.ltCreateTime,
                'payType':_this.payType,
                'orderStatus':_this.orderStatus,
                'size':_this.size,
                'page':_this.page - 1,
            }
            queryVipOrder(params)
            .then(res=>{
                _this.isload = false
                if(res.success){
                    _this.dataList = res.data.content
                    _this.total = res.data.totalElements
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 分页
        handleTableChange(pagination, filters, sorter) {
            console.log(pagination);
            this.page = pagination.current;
            this.size = pagination.pageSize;
            this.getData()
        },
        // 添加
        add(){

            this.item={
                id: '',
                productName:"",//名称	
                activeDay:"",//可用天数	
                price:"",//优惠前价格		
                realPrice:"",//当前价格				
                isOn:1,//是否开启,0:否,1:是
            },
            this.visible = true
        },
        // 修改
        update(item){
            this.item = JSON.parse(JSON.stringify(item))
          this.visible = true
          
        },
        // 确定添加、删除
        handleOk(){
          
            let _this = this
            const params = {
                'brandId':_this.brandId,
                'vipProductId':_this.vipProductId,
                'phone':_this.phone,
            }
            if(!_this.brandId){
                this.$message.warning('请先选择品牌!')
                return
            }
            if(_this.vipProductId===''){
                this.$message.warning('请先选择VIP产品!')
                return
            }
             if(_this.phone.length!=11){
                this.$message.warning('请先输入正确的手机号!')
                return
            }
            updateVipOrder(params)
            .then(res=>{
                if(res.success){
                    _this.getData()
                    _this.phone=''
                    _this.$notification['success']({
                        message: '提示',
                        description: _this.item.id ? '修改成功' : '添加成功',
                        duration: 8,
                    })
                    _this.visible = false
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 删除
        del(item){
            let _this = this
            const params = {
                'vipOrderFormId':item.id,
                'isDelete':1,
            }
            updateVipOrderFrom(params)
            .then(res=>{
                if(res.success){
                    _this.getData()
                    _this.$notification['success']({
                        message: '提示',
                        description: '删除成功',
                        duration: 8,
                    })
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
    }
}

</script>
<style scoped>

</style>